#!/bin/bash

# 显示彩色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 设置绝对路径
SYNOLOGY_PATH="/volume1/docker/hello-beer-pos"

echo -e "${GREEN}=========================================${NC}"
echo -e "${GREEN}   Hello Beer POS 系统重启脚本 (Docker Compose版)   ${NC}"
echo -e "${GREEN}=========================================${NC}"

# 拉取最新代码
echo -e "${GREEN}拉取最新代码...${NC}"
cd $SYNOLOGY_PATH
git pull

# 检查.env文件是否存在
if [ ! -f "$SYNOLOGY_PATH/.env" ]; then
    echo -e "${RED}错误: 未找到.env文件，请先运行部署脚本或手动创建.env文件${NC}"
    exit 1
else
    echo -e "${GREEN}已找到.env文件，将使用现有配置${NC}"
fi

# 询问是否需要重建前端
read -p "是否需要重建前端? (y/n): " rebuild_frontend
if [[ $rebuild_frontend == "y" || $rebuild_frontend == "Y" ]]; then
  echo -e "${GREEN}重建前端...${NC}"
  cd $SYNOLOGY_PATH/frontend
  npm install
  npm run build
  cd $SYNOLOGY_PATH
  
  # 重启前端容器
  echo -e "${YELLOW}重启前端容器...${NC}"
  docker-compose -f docker-compose.mount.yml restart frontend
fi

# 询问是否需要重启后端
read -p "是否需要重启后端? (y/n): " restart_backend
if [[ $restart_backend == "y" || $restart_backend == "Y" ]]; then
  echo -e "${YELLOW}重启后端容器...${NC}"
  docker-compose -f docker-compose.mount.yml restart backend
  
  # 显示后端日志
  echo -e "${GREEN}显示后端日志（按Ctrl+C退出）:${NC}"
  docker-compose -f docker-compose.mount.yml logs -f backend
fi

echo -e "${GREEN}操作完成!${NC}" 